<?php /* УТФ-8:fix */

class SPImodel__media extends SPImodel__dummy
{
    public function get_public_source_by_media_item( $media_item, $attrs = null )
    {
        return $this->get_public_source( $media_item, $attrs );
    }
/*
    public function get_public_source_by_id( $media_id, $attrs = null )
    {
        return $this->get_public_source( $this->get_by_id( $media_id ), $attrs );
    }
*/

    public function get_public_sourceByIndexOld( $media_gid, $parent_object_id, $index = 1, $attrs = null )
    {
      return $this->get_public_source( $this->get_by_index( $media_gid, $parent_object_id, $index ), $attrs );
    }

    public function get_public_sourceByIndexNewJpeg( $media_gid, $parent_object_id, $index = 1, $attrs = null )
    {
        $addvars = '';
        $source = SPIcfg::$dir['pubsite_root']
                . SPIcfg::$media['preview_script'] . '/'
                . "p-{$media_gid}-{$parent_object_id}-{$index}/"
                ;

        if( $attrs['mode'] == 'preview' )
        {
            $width  = isset( $attrs['width']  ) ? (int) $attrs['width']  : '$WIDTH$';
            $height = isset( $attrs['height'] ) ? (int) $attrs['height'] : '$HEIGHT$';

            $addvars.= $width   ? "&fw={$width}"  : '';
            $addvars.= $height  ? "&fh={$height}" : '';

            $source .= 'preview/';
        }

        if( $attrs['friendlyname'] )
        {
            $addvars.= '&friendlyname='.urlencode( $attrs['friendlyname'] );
        }

        if( $attrs['download'] === true )
        {
            $addvars.= '&download=true';
        }

        $source.= "file_{$media_gid}_{$parent_object_id}_{$index}.jpg"
                . ( $addvars ? '?' . ltrim( $addvars, '&' ) : '' )
                ;

        return $source;
    }

    public function get_public_source( $media_item, $attrs )
    {
        if( $media_item === false )
        {
            return false;
        }

        $addvars = '';
        $source = SPIcfg::$dir['pubsite_root']
                . SPIcfg::$media['preview_script'] . '/'
                . 'i-' . $media_item['id'] . '-' . strtotime( isset( $media_item['t_updated'] ) ? $media_item['t_updated'] : $media_item['t_created'] ) . '/'
                ;

        if( $attrs['mode'] == 'preview' )
        {
            $width  = isset( $attrs['width']  ) ? (int) $attrs['width']  : '$WIDTH$';
            $height = isset( $attrs['height'] ) ? (int) $attrs['height'] : '$HEIGHT$';

            $addvars.= $width   ? "&fw={$width}"  : '';
            $addvars.= $height  ? "&fh={$height}" : '';

            $source .= 'preview/';
        }

        if( $attrs['friendlyname'] )
        {
            $addvars.= '&friendlyname='.urlencode( $attrs['friendlyname'] );
        }

        if( $attrs['download'] === true )
        {
            $addvars.= '&download=true';
        }

        $source.= 'file.' . $media_item['id'] . '.' . $media_item['extension']
                . ( $addvars ? '?' . ltrim( $addvars, '&' ) : '' )
                ;

        return $source;
    }

    public function get_filesystem_source( $mid, $is_auto_mkdir = false )
    {
        $folder = SPIcfg::$media['basedir'] . floor( $mid / 1000 ) . '/';

        if( $is_auto_mkdir === true && is_dir( $folder ) === false )
        {
            mkdir( $folder );
        }

        return $folder.'media_'.$mid.'.file';
    }

    public function clean_cache( $media_item )
    {
        $cache_folder = SPIcfg::$media['cache_basedir'] . floor( $media_item['id'] / 1000 ) . '/';

        if( is_dir( $cache_folder ) === false )
        {
            return true;
        }

        $hd = opendir( $cache_folder );

        if( is_resource( $hd ) === false )
        {
            return false;
        }

        while( $file = readdir( $hd ) )
        {
            if( $file == '.' || $file == '..' )
            {
                continue;
            }

            if( preg_match( '/^media_'.$media_item['id'].'([_.])/u', $file ) > 0 )
            {
                unlink( $cache_folder . $file );
            }
        }
        closedir( $hd );

        return true;
    }

    public function get_cache_source( $media_item, $cache_attrs = false, $ext = 'cache' )
    {
        $cache_folder = SPIcfg::$media['cache_basedir'] . floor( $media_item['id'] / 1000 ) . '/';

        if( is_dir( $cache_folder ) === false )
        {
            mkdir( $cache_folder );
        }

        $cache_filename = 'media_'.$media_item['id'];

        if( is_array( $cache_attrs ) === true )
        foreach( $cache_attrs as $key => $value )
        {
            $cache_filename .= "_{$key}{$value}";
        }
        $cache_filename .= ( $ext ? ".{$ext}" : ".cache" );

        return $cache_folder.$cache_filename;
    }

    public function get_cache_source_static( $source_prefix, $cache_attrs = false, $ext = 'cache' )
    {
        $cache_folder = SPIcfg::$media['cache_basedir'];

        if( is_dir( $cache_folder ) === false )
        {
            mkdir( $cache_folder );
        }

        $cache_filename = $source_prefix;

        if( is_array( $cache_attrs ) === true )
        foreach( $cache_attrs as $key => $value )
        {
            $cache_filename .= "_{$key}{$value}";
        }
        $cache_filename .= ( $ext ? ".{$ext}" : ".cache" );

        return $cache_folder.$cache_filename;
    }

    public function get_by_id( $media_id )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:media']
               .     " WHERE id=".(int) $media_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function get_by_index( $media_gid, $parent_object_id, $index = 1 )
    {
        $parent_object_id = (int) $parent_object_id;
        $index            = max( 1, (int) $index );
        $offset           = $index-1;

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:media']
               .     " WHERE gid='" . addslashes( $media_gid ) . "'"
               .       " AND pid={$parent_object_id} "
               .  " ORDER BY order_index "
               .   " LIMIT 1 OFFSET {$offset} "
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function get_profile_avatar( $profile_id, $forum_user_id )
    {
        // used only 1 value from this vars:
        $profile_id    = (int) $profile_id;
        $forum_user_id = (int) $forum_user_id;

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:media']
               .     " WHERE gid='" . SPIc::I('profiles')->MEDIA_GID . "'"
               . ( $profile_id
                 ? " AND pid={$profile_id} "
                 : ( $forum_user_id
                   ? " AND pid=(   SELECT id "
                               . "   FROM " . SPIcfg::$store['table:profiles']
                               . "  WHERE forum_user_id={$forum_user_id} "
                               . "  LIMIT 1 "
                               . " ) "
                   : " AND FALSE " 
                   )
                 )
               .  " ORDER BY order_index "
               .   " LIMIT 1 "
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function get_count( $gid, $pid )
    {
        $pid = (int) $pid;

        $query =    " SELECT COUNT(*) AS count "
               .      " FROM ".SPIcfg::$store['table:media']
               .     " WHERE gid='" . addslashes( $gid ) . "' AND " . ( $pid == 0 ? " pid IS NULL " : " pid={$pid} " )
               ;

        $item = SPIstore::I()->query( $query )->fetch_and_close();

        return $item['count'];
    }

    public function select( $gid, $pid )
    {
        $pid = (int) $pid;

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:media']
               .     " WHERE gid='" . addslashes( $gid ) . "' AND " . ( $pid == 0 ? " pid IS NULL " : " pid={$pid} " )
               .  " ORDER BY order_index "
               ;

        return SPIstore::I()->query( $query );
    }
}
